unit clscontato;

interface

uses Dm, dbtables, biblio, Grids, Sysutils;

type
  TContato = class(TObject)
  private
    Ent_Codigo: string;
    Cont_Codigo: string;
  public
    Cont_Nome: string;
    Cont_Departamento: string;
    Cont_Ramal: string;
    Cont_email: string;
    Cont_Cargo: string;
    Cont_Nasc: string;
    Cont_Fone: string;
    function Gravar(p_Ent_Codigo, p_Cont_Codigo: string): Byte;
    function Recuperar(p_Ent_Codigo, p_Cont_Codigo: string): Byte;
    function Recuperar_Primeiro(p_Ent_Codigo: string): Boolean;
    function Excluir(p_Ent_Codigo: string): Byte;
    procedure Consultar(var p_q: TQuery; p_Ent_Codigo: string);
    procedure Retorna_Contatos(Entidade: string; var sg: TStringGrid);
    function Ultimo(Entidade: string): string;
  end;

implementation

uses DB;

function TContato.Gravar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Contato where Ent_Codigo=:k_Ent_Codigo and Cont_Codigo=:k_Cont_Codigo');
  tq.parambyname('k_Ent_Codigo').asstring := strzero(p_Ent_Codigo, 5);
  tq.parambyname('k_Cont_Codigo').asstring := strzero(p_Cont_Codigo, 2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO Contato VALUES(' +
      ':k_Ent_Codigo,' +
      ':k_Cont_Codigo,' +
      ':k_Cont_Nome,' +
      ':k_Cont_Departamento,' +
      ':k_Cont_Ramal,' +
      ':k_Cont_email,' +
      ':k_Cont_Cargo,' +
      ':K_cont_nasc,' +
      ':k_Cont_Fone)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE Contato SET ' +
      'Cont_Codigo=:k_Cont_Codigo,' +
      'Cont_Nome=:k_Cont_Nome,' +
      'Cont_Departamento=:k_Cont_Departamento,' +
      'Cont_Ramal=:k_Cont_Ramal,' +
      'Cont_email=:k_Cont_email,' +
      'Cont_Cargo=:k_Cont_Cargo,' +
      'cont_nasc=:K_cont_nasc,' +
      'Cont_Fone=:k_Cont_Fone ' +
      'where Ent_Codigo=:k_Ent_Codigo and Cont_Codigo=:k_Cont_Codigo');
  end;
  tq.parambyname('k_Ent_Codigo').asstring := strzero(p_ent_codigo, 5);
  tq.parambyname('k_Cont_Codigo').asstring := strzero(p_Cont_Codigo, 2);
  tq.parambyname('k_Cont_Nome').asstring := Cont_Nome;
  tq.parambyname('k_Cont_Departamento').asstring := Cont_Departamento;
  tq.parambyname('k_Cont_Ramal').asstring := Cont_Ramal;
  tq.parambyname('k_Cont_email').asstring := Cont_email;
  tq.parambyname('k_Cont_Cargo').asstring := Cont_Cargo;
  tq.parambyname('k_Cont_nasc').AsString := Cont_Nasc;
  tq.parambyname('k_Cont_Fone').AsString := Cont_Fone;
  tq.close;
  tq.execsql;
  result := 1;
  tq.Free;
end;

function TContato.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dm.dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Contato where Ent_Codigo=:k_Ent_Codigo and Cont_Codigo=:k_Cont_Codigo');
  tq.parambyname('k_Ent_Codigo').asstring := strzero(p_Ent_Codigo, 5);
  tq.parambyname('k_Cont_Codigo').asstring := strzero(p_Cont_Codigo, 2);
  tq.open;
  Ent_Codigo := p_Ent_Codigo;
  Cont_Codigo := p_Cont_Codigo;
  if tq.eof then begin
    Result := 0;
  end else begin
    Result := 1;
  end;
  Cont_Nome := tq.fieldbyname('Cont_Nome').asstring;
  Cont_Departamento := tq.fieldbyname('Cont_Departamento').asstring;
  Cont_Ramal := tq.fieldbyname('Cont_Ramal').asstring;
  Cont_email := tq.fieldbyname('Cont_email').asstring;
  Cont_Cargo := tq.fieldbyname('Cont_Cargo').asstring;
  Cont_Nasc := tq.fieldbyname('Cont_nasc').asstring;
  Cont_Fone := tq.fieldbyname('Cont_Fone').AsString;
  tq.Close;
  tq.Free;
end;

function TContato.Excluir;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM Contato where Ent_Codigo=:k_Ent_Codigo');
  tq.parambyname('k_Ent_Codigo').asstring := strzero(p_Ent_Codigo, 5);
  tq.execSQL;
  Result := 1;
  tq.close;
  tq.Free;
end;

procedure TContato.Consultar;
begin
  p_q.sql.Clear;
  p_q.Params.Clear;
  p_q.sql.add('Select * from Contato where Ent_Codigo=:k_Ent_Codigo');
  p_q.parambyname('k_Ent_Codigo').asstring := strzero(p_Ent_Codigo, 5);
end;

procedure TContato.Retorna_Contatos;
var
  tq: TQuery;
  i: integer;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from Contato where Ent_Codigo=:a');
  tq.params[0].asstring := Entidade;
  tq.open;
  i := 1;
  sg.rowcount := 2;
  sg.cells[0, 1] := '';
  sg.cells[1, 1] := '';
  while not tq.eof do begin
    if i > 1 then sg.rowcount := sg.rowcount + 1;
    sg.cells[0, i] := tq.fieldbyname('Cont_Nome').asstring;
    sg.cells[1, i] := tq.fieldbyname('Cont_Email').asstring;
    i := i + 1;
    tq.next;
  end;
  tq.close;
  tq.Free;
end;

function TContato.Recuperar_Primeiro;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Contato where Ent_Codigo=:k_Ent_Codigo');
  tq.parambyname('k_Ent_Codigo').asstring := strzero(p_Ent_Codigo, 5);
  tq.open;
  Result := not tq.eof;
  Cont_Nome := tq.fieldbyname('Cont_Nome').asstring;
  Cont_Departamento := tq.fieldbyname('Cont_Departamento').asstring;
  Cont_Ramal := tq.fieldbyname('Cont_Ramal').asstring;
  Cont_email := tq.fieldbyname('Cont_email').asstring;
  Cont_Cargo := tq.fieldbyname('Cont_Cargo').asstring;
  Cont_Nasc := tq.fieldbyname('Cont_nasc').asstring;
  Cont_Fone := tq.fieldbyname('Cont_Fone').AsString;
  tq.Close;
  tq.Free;
end;

function TContato.Ultimo(Entidade: string): string;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select max(Cont_Codigo) as Ult from Contato ' +
    'where Ent_codigo = ' + #39 + Entidade + #39);
  tq.Open;
  if tq.FieldByName('Ult').IsNull then
    Result := '01'
  else
    Result := StrZero(StrToInt(tq.FieldByName('Ult').AsString) + 1, 2);
  tq.close;
  tq.Free;
end;

end.
